import { BodyParam, Delete, Get, JsonController, Param, Post, Put, QueryParam } from "routing-controllers";
import { Inject } from "typedi";
import RoleService from '../services/RoleService';
import Response from '../common/response';

@JsonController("/role")
export default class RoleController {
    @Inject()
    roleService: RoleService
    @Post()
    async addRole(@BodyParam("roleName", { required: true }) roleName: string,
        @BodyParam("roleDesc", { required: true }) roleDesc: string,
        @BodyParam("menuIds") menuIds: string[]) {
        const role = await this.roleService.selectRoleByRoleName(roleName)
        if (role) {
            return Response.BAD_REQUEST("角色已经存在");
        }
        await this.roleService.addRole(roleName, roleDesc, menuIds)
        return Response.SUCCESS()
    }
    @Get("/getById/:roleId")
    async getRoleById(@Param("roleId") roleId: number) {
        const role = await this.roleService.getRoleById(roleId)
        return Response.SUCCESS(role)
    }
    @Delete("/:id")
    async delRole(@Param("id") id: number) {
        try {
            await this.roleService.delRole(id)
        } catch (err) {
            return Response.BAD_REQUEST()
        }
        return Response.SUCCESS()
    }
    @Put()
    async updateRole(
        @BodyParam("id", { required: true }) id: number,
        @BodyParam("roleName", { required: true }) roleName: string,
        @BodyParam("roleDesc", { required: true }) roleDesc: string,
        @BodyParam("dataStatus") dataStatus: number,
        @BodyParam("menuIds") menuIds: string[]) {
        await this.roleService.updateRole(id, roleName, roleDesc, menuIds, dataStatus)
        return Response.SUCCESS()
    }
    @Get("/list")
    async getAllRoleList(
        @QueryParam("roleName") roleName: string,
        @QueryParam("roleDesc") roleDesc: string,
        @QueryParam("createTimeRange") createTimeRange: string[],
        @QueryParam("updateTimeRange") updateTimeRange: string[],
        @QueryParam("dataStatus") dataStatus: number,
        @QueryParam("page") page: number,
        @QueryParam("pageSize") pageSize: number) {
        let data = await this.roleService.getRoleByConds(roleName,
            roleDesc,
            dataStatus,
            createTimeRange,
            updateTimeRange,
            page,
            pageSize)
        return Response.SUCCESS(data)
    }
    @Get("/all")
    async getAllRole() {
        const data = await this.roleService.getAllRole()
        return Response.SUCCESS(data)
    }
}